//
//  ImageManager.m
//  MyMoon
//
//  Created by ZHANGXIANG on 15-6-30.
//  Copyright (c) 2015年 ZHANGXIANG. All rights reserved.
//
#import "UIImage+Customer.h"
#import "ImageManager.h"
#import "UIButton+WebCache.h"
#import "UIImageView+WebCache.h"

@implementation ImageManager

+ (id)shareImageManager
{
    static ImageManager *sharedInstance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedInstance = [[ImageManager alloc] init];
    });
    
    return sharedInstance;
}

+ (CGSize)getSizeWithImage:(UIImage *)image withContainSize:(CGSize)size
{
    float width = 0;
    float height = 0;
    
    CGSize imgSize = image.size;
    float widthRate = imgSize.width/size.width;
    float heightRate = imgSize.height/size.height;
    if(widthRate >= heightRate){
        width = size.width;
        if(imgSize.width < size.width){
            width = imgSize.width;
        }
        height = imgSize.height*width/imgSize.width;
    }else{
        height = size.height;
        if(imgSize.height < size.height){
            height = imgSize.height;
        }
        width = imgSize.width*height/imgSize.height;
    }
    
    return CGSizeMake(width, height);
}
- (void)setImageWithUrl:(NSString *)urlStr defaultImage:(UIImage *)image inView:(UIView *)view
{
    if([view isKindOfClass:[UIImageView class]])
    {
        UIImageView *imgView = (UIImageView *)view;
        imgView.contentMode = UIViewContentModeScaleAspectFill;
        imgView.clipsToBounds = YES;
        [imgView sd_setImageWithURL:[NSURL URLWithString:urlStr] placeholderImage:image];
    }else if([view isKindOfClass:[UIButton class]]){
        UIButton *imgBtn = (UIButton *)view;
        imgBtn.contentMode = UIViewContentModeScaleAspectFill;
        imgBtn.clipsToBounds = YES;
        [imgBtn sd_setImageWithURL:[NSURL URLWithString:urlStr] forState:UIControlStateNormal placeholderImage:image];
    }else{
        UIImageView *imgView = (UIImageView *)[view viewWithTag:55555];
        if(!imgView){
            imgView = [[UIImageView alloc] init];
            imgView.tag = 55555;
            [view addSubview:imgView];
        }
        imgView.contentMode = UIViewContentModeScaleAspectFill;
        
        [imgView sd_setImageWithURL:[NSURL URLWithString:urlStr] placeholderImage:image];
        imgView.frame = view.bounds;
    }
}

- (void)setImageWithUrl:(NSString *)urlStr defaultImage:(UIImage *)image cornerRadius:(float)dius inView:(UIView *)view 
{
    if([view isKindOfClass:[UIImageView class]])
    {
        UIImageView *imgView = (UIImageView *)view;
        imgView.contentMode = UIViewContentModeScaleAspectFill;
        imgView.layer.cornerRadius = dius;
        imgView.clipsToBounds = YES;
        [imgView sd_setImageWithURL:[NSURL URLWithString:urlStr] placeholderImage:image];
    }else if([view isKindOfClass:[UIButton class]]){
        UIButton *imgBtn = (UIButton *)view;
        imgBtn.contentMode = UIViewContentModeScaleAspectFill;
        imgBtn.layer.cornerRadius = dius;
        imgBtn.clipsToBounds = YES;
        [imgBtn sd_setImageWithURL:[NSURL URLWithString:urlStr] forState:UIControlStateNormal placeholderImage:image];
    }else{
        UIImageView *imgView = (UIImageView *)[view viewWithTag:55555];
        if(!imgView){
            imgView = [[UIImageView alloc] init];
            imgView.tag = 55555;
            [view addSubview:imgView];
        }
        imgView.contentMode = UIViewContentModeScaleAspectFill;
        imgView.layer.cornerRadius = dius;
        view.layer.cornerRadius = dius;
        imgView.clipsToBounds = YES;
        view.clipsToBounds = YES;
        
        [imgView sd_setImageWithURL:[NSURL URLWithString:urlStr] placeholderImage:image];
        imgView.frame = view.bounds;
    }
}

- (void)setImageWithUrl:(NSString *)urlStr withImageType:(WF_QiNiu_Image_Resize_Type)type withBlur:(float)blur defaultImage:(UIImage *)defaultImage cornerRadius:(float)dius inView:(UIView *)view
{
    
    NSString *newImgUrl = nil;
    switch (type) {
        case WF_QiNiu_Image_Resize_Type_Original:{
            //默认原图
            newImgUrl = urlStr;
        }
            break;
//        case WF_QiNiu_Image_Resize_Type_Small:{
//            //小
//            newImgUrl=[urlStr adjust2SmallImageUrlString];
//        }
//            break;
//        case WF_QiNiu_Image_Resize_Type_Middle:{
//            //中
//            newImgUrl = [urlStr adjust2MiddleImageUrlString];
//        }
//            break;
//        case WF_QiNiu_Image_Resize_Type_Large:{
//            //大
//            newImgUrl =  [urlStr adjust2LargeImageUrlString];
//        }
//            break;
        default:
        {
             newImgUrl = [NSString stringWithFormat:@"%@?imageView2/",urlStr];
        }
        break;
    }

    if([view isKindOfClass:[UIImageView class]])
    {
        UIImageView *imgView = (UIImageView *)view;
        imgView.contentMode = UIViewContentModeScaleAspectFill;
        imgView.layer.cornerRadius = dius;
        imgView.clipsToBounds = YES;
        
        __block UIImageView *tmpImg = imgView;
        
        [imgView sd_setImageWithURL:[NSURL URLWithString:newImgUrl] placeholderImage:defaultImage completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
            if(image){
                if(blur > 0){
                    tmpImg.image = [image blurryImageWithBlurLevel:blur];
                }
            }
        }];
    }else if([view isKindOfClass:[UIButton class]]){
        UIButton *imgBtn = (UIButton *)view;
        imgBtn.layer.cornerRadius = dius;
        imgBtn.clipsToBounds = YES;
        
        __block UIButton *tmpBtn = imgBtn;
        [imgBtn sd_setBackgroundImageWithURL:[NSURL URLWithString:newImgUrl] forState:UIControlStateNormal placeholderImage:defaultImage completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
            if(image){
                if(blur > 0){
                    [tmpBtn setBackgroundImage:[image blurryImageWithBlurLevel:blur] forState:UIControlStateNormal];
                }
            }
        }];
    }else{
        UIImageView *imgView = (UIImageView *)[view viewWithTag:55555];
        if(!imgView){
            imgView = [[UIImageView alloc] init];
            imgView.tag = 55555;
            [view addSubview:imgView];
        }
        imgView.contentMode = UIViewContentModeScaleAspectFill;
        imgView.layer.cornerRadius = dius;
        view.layer.cornerRadius = dius;
        imgView.clipsToBounds = YES;
        view.clipsToBounds = YES;
        imgView.frame = view.bounds;
        
        __block UIImageView *tmpImg = imgView;
        [imgView sd_setImageWithURL:[NSURL URLWithString:newImgUrl] placeholderImage:defaultImage completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
            if(image){
                if(blur > 0){
                    tmpImg.image = [image blurryImageWithBlurLevel:blur];
                }
            }
        }];
    }
}

@end
